package com.presto.directo.mvvm.viewmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.presto.directo.entity.ContactEntity
import com.presto.directo.net.getHttpMap
import com.presto.directo.net.sign
import com.presto.directo.utils.*

class ContactViewModel(application: Application) : BaseViewModel(application) {

    var relationship1 = -1
    var relationship2 = -1

    val infoLiveData = MutableLiveData<ContactEntity>()
    val uploadLiveData = MutableLiveData<Any>()


    fun getInfo() {
        start(onNext = {
            showProgress()
            getApiService().getContact(getHttpMap().sign(25)).getResultOrNull()?.let {
                relationship1 = it.relation_1.toIntOrNull() ?: -1
                relationship2 = it.relation_2.toIntOrNull() ?: -1
                infoLiveData.value = it
            }
        }, onComplete = {
            dismissProgress()
        })
    }

    fun upload(map: MutableMap<String, Any?>) {
        start(onNext = {
            showProgress()
            getApiService().saveContract(map.sign(24)).getResultOrNull()
            uploadLiveData.value = true
        }, onComplete = {
            dismissProgress()
        })
    }
}